﻿/*----------------------------------------------------------------
// Copyright (C) 2009 
// 版权所有。 
//
// 文件名：
// 文件功能描述：考勤信息统计
//
//
// 创建标识：
// 程序作者：翟允锋
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;
using Wuqi.Webdiyer;

public partial class TimeRecord : System.Web.UI.Page
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private DaActLog daLog = new DaActLog();
    private DaDept dalDept = new DaDept();
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        if (!IsPostBack)
        {
            DataSet dsDept = dalDept.Query(string.Empty);
            Utility.BindDataSet2Combox(dsDept, DDLdept, "Dept_Name", "Dept_ID");
            DDLdept.Items.Insert(0, new ListItem("", ""));

            for (int i = 1; i <= 12; i++)
            {
                DdlMonth.Items.Add(new ListItem(i.ToString() + "月份", string.Format("{0:D2}", i)));
            }
            DdlMonth.SelectedValue = string.Format("{0:D2}", DateTime.Now.Month);
            this.BtnQuery_Click(null, null);
        }
    }

    protected void BtnQuery_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView.Columns.Count; i++)
        {
            GridView.Columns[i].Visible = true;
        }

        DataSet dsRecords;

        if (string.IsNullOrEmpty(DDLdept.SelectedValue))
        {
            dsRecords = daLog.GetTimeBookByMonth(DateTime.Now.Year.ToString() + DdlMonth.SelectedValue);
        }
        else
        {
            dsRecords = daLog.GetTimeBookByDeptPerMonth(DateTime.Now.Year.ToString() + DdlMonth.SelectedValue, DDLdept.SelectedValue);
        }
        GridView.DataSource = dsRecords;
        GridView.DataBind();
        if (dsRecords.Tables[0].Rows.Count > 0)
        {
            int days = string.IsNullOrEmpty(DdlMonth.SelectedValue) ? 31 : DateTime.DaysInMonth(DateTime.Now.Year, int.Parse(DdlMonth.SelectedValue));


            for (int i = 4; i < 35; i++)
            {
                if (string.IsNullOrEmpty(DdlMonth.SelectedValue))
                {
                    GridView.HeaderRow.Cells[i].Text = Convert.ToString(i - 3) + "日";
                }
                else
                { 
                    GridView.HeaderRow.Cells[i].Text = int.Parse(DdlMonth.SelectedValue).ToString() + "月" + Convert.ToString(i - 3) + "日<br/>" +
                        ServerValidate.GetWeekDay(DateTime.Now.Year.ToString() + "-" + int.Parse(DdlMonth.SelectedValue).ToString() + "-" + Convert.ToString(i - 3));
                }

                GridView.HeaderRow.Cells[i].HorizontalAlign = HorizontalAlign.Center;
                GridView.HeaderRow.Cells[i].Width = 60;
                GridView.HeaderRow.Cells[i].ForeColor = System.Drawing.Color.White;
            }

            for (int i = 31; i > days; i--)
            {
                GridView.Columns[3 + i].Visible = false;
            }

            GridView.Width = 2400;
        }
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        DataSet dsRecords;

        if (string.IsNullOrEmpty(DDLdept.SelectedValue))
        {
            dsRecords = daLog.GetTimeBookByMonth(DateTime.Now.Year.ToString() + DdlMonth.SelectedValue);
        }
        else
        {
            dsRecords = daLog.GetTimeBookByDeptPerMonth(DateTime.Now.Year.ToString() + DdlMonth.SelectedValue, DDLdept.SelectedValue);
        }

        string filename = @"Kqin.xls";

        GridViewExportUtil.Export(filename, this.GridView);
    }
}
